package com.hhkj.admin.order.service.base.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.order.common.cont.OrderItemConst;
import com.hhkj.admin.order.common.cont.OrderRefundConst;
import com.hhkj.admin.order.domain.Order;
import com.hhkj.admin.order.domain.OrderItem;
import com.hhkj.admin.order.mapper.OrderItemMapper;
import com.hhkj.admin.order.model.dopo.OrderItemDo;
import com.hhkj.admin.order.model.dopo.OrderItemPo;
import com.hhkj.admin.order.service.base.OrderItemService;
import com.hhkj.admin.order.service.base.OrderService;
import com.hhkj.admin.student.domain.StudentCourse;
import com.hhkj.admin.student.service.base.StudentCourseService;
import com.hhkj.admin.student.service.base.StudentLoginLogService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class OrderItemServiceImpl
        extends BaseServiceImpl<OrderItem, OrderItemDo, OrderItemPo, OrderItemMapper>
        implements OrderItemService {

    @Autowired
    private StudentLoginLogService studentLoginLogService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private StudentCourseService studentCourseService;

    @Override
    public List<OrderItem> listByIdList(List<Long> idList) {
        if (ObjectUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        idList = CollectionUtil.distinct(idList);
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<OrderItem>()
                .in(OrderItem::getId, idList);
        return list(queryWrapper);
    }


    @Override
    public boolean updateRefundStatusBatch(List<Long> orderItemIdList, OrderRefundConst.StatusEnum statusCode) {
        LambdaUpdateWrapper<OrderItem> updateWrapper = new LambdaUpdateWrapper<OrderItem>()
                .in(OrderItem::getId, orderItemIdList)
                .set(OrderItem::getRefundStatusCode, statusCode.getCode());
        return this.update(updateWrapper);
    }

    @Override
    public List<OrderItemPo> fillStudentCourseStatus(List<OrderItemPo> orderItemPoList) {
        List<OrderItemPo> poList = this.from(orderItemPoList).join(
                studentCourseService,
                StudentCourseService::listByIdList,
                OrderItemPo::getStudentCourseId,
                StudentCourse::getId,
                StudentCourse::getStatus,
                OrderItemPo::setStudentCourseStatus
        );
        return poList;
    }

    @Override
    public List<OrderItemPo> fillLoginFlag(List<OrderItemPo> orderItemPoList) {
        if (ObjectUtil.isEmpty(orderItemPoList)) {
            return new ArrayList<>();
        }
        Order order = orderService.getById(CollectionUtil.getFirst(orderItemPoList).getOrderId());
        List<String> idCardList = orderItemPoList.stream().map(OrderItem::getIdCard).collect(Collectors.toList());
        Map<String, Set<Long>> idCardCourseIdSetMap = studentLoginLogService.courseIdSetGroupByIdCard(idCardList, order.getPayTime(), null);
        orderItemPoList.forEach(orderItemPo -> {
            Set<Long> courseIdSet = idCardCourseIdSetMap.get(orderItemPo.getIdCard());
            if (ObjectUtil.isNotEmpty(courseIdSet) && courseIdSet.contains(orderItemPo.getCourseId())) {
                orderItemPo.setLoginFlag(OrderItemConst.LoginFlag.YES.getCode());
            } else {
                orderItemPo.setLoginFlag(OrderItemConst.LoginFlag.NO.getCode());
            }
        });
        return orderItemPoList;
    }
}




